package com.link.base.wechat.basic;
/**
 * @author lujiang
 */


import com.link.core.basic.query.QueryParams;
import com.link.core.modules.imports.FileReader;
import com.link.core.modules.imports.ImportReader;
import com.link.core.util.AppConstants;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

public class ImportsFactoryStore {

    public static ImportReader newInstance(QueryParams qps, HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile myfile = multipartRequest.getFile("importFile");
        FileReader fileReader = null;
        String fileName = myfile.getOriginalFilename();
        String fileext = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        fileext = fileext.toLowerCase();
        String xlsSufix = ".xls";
        String xlsxSufix = ".xlsx";
        if (xlsSufix.equals(fileext) || xlsxSufix.equals(fileext)) {
            fileReader = new StoreExcelFileReader();
        }
        CommonsMultipartFile cf = (CommonsMultipartFile) myfile;
        DiskFileItem fi = (DiskFileItem) cf.getFileItem();
        File file = fi.getStoreLocation();
        if (null != fileReader) {
            fileReader.setEncoding(AppConstants.APP_EXPORT_ENCODING);
            return new ImportReader(qps, file, fileReader, fileext);
        } else {
            return new ImportReader(qps, file, null, fileext);
        }
    }

}
